/*
 * XReflectObject.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.util.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 
 * 
 *
 * @author Devon Carew
 */
public class XReflectObject
{
    private Object  object;
    private Class   clazz;
    
    public static XReflectObject createReflectObject(Object object)
    {
        return new XReflectObject(object);
    }
    
    
    XReflectObject(Object reflectObject)
    {
        this.object = reflectObject;
        
        clazz = object.getClass();
    }
    
    
    public Object getReflectObject()
    {
        return object;
    }
    
    
    public Object invoke(String methodName)
    {
        return invokeVarArgs(methodName, null);
    }
    
    public Object invoke(String methodName, Object arg1)
    {
        return invokeVarArgs(methodName, new Object[] { arg1 });
    }
    
    public Object invoke(String methodName, Object arg1, Object arg2)
    {
        return invokeVarArgs(methodName, new Object[] { arg1, arg2 });
    }
    
    private static Object[] EMPTY_ARRAY = new Object[0];
    
    public Object invokeVarArgs(String methodName, Object[] args)
    {
        if (args == null)
            args = EMPTY_ARRAY;
        
        Method method = XReflectClass.lookupMethod(clazz, methodName, args, false);
        
        if (method == null)
        	throw new RuntimeException("Unable to locate method " + methodName);
        
        try
        {
            return method.invoke(object, args);
        }
        catch (Throwable t)
        {
            if (t instanceof RuntimeException)
                throw (RuntimeException)t;
            
            throw new RuntimeException(t);
        }
    }
    
    // field access
    
    public void setField(String fieldName, Object value)
    {
    	Field field = XReflectClass.lookupField(clazz, fieldName, false);
    	
    	if (field == null)
    		throw new RuntimeException("Unable to locate field " + fieldName);
    	
    	try
		{
			field.set(object, value);
		}
        catch (Throwable t)
        {
            if (t instanceof RuntimeException)
                throw (RuntimeException)t;
            
            throw new RuntimeException(t);
        }
    }
    
    public Object getField(String fieldName)
    {
    	Field field = XReflectClass.lookupField(clazz, fieldName, false);
    	
    	if (field == null)
    		throw new RuntimeException("Unable to locate field " + fieldName);
    	
    	try
		{
			return field.get(object);
		}
        catch (Throwable t)
        {
            if (t instanceof RuntimeException)
                throw (RuntimeException)t;
            
            throw new RuntimeException(t);
        }
    }
    
}
